package org.e_gold.dao.impl;

import org.e_gold.dao.SettledContractDAO;
import org.e_gold.model.SettledContract;
import org.e_gold.util.SQL;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class SettledContractDAOImpl extends AbstractGenericDAO<SettledContract> implements SettledContractDAO, SQL {

    public SettledContractDAOImpl() {

        super(SettledContract.class);
    }

    @Override
    @Transactional
    public SettledContract getRunningSettledContract() {

        SettledContract retval = null;
        Criteria criteria = getCurrentSession().createCriteria(SettledContract.class);
        criteria.setFirstResult(0);
        criteria.add(Restrictions.eq(SettledContract.RUNNING, true));
        if (criteria.list() != null && criteria.list().size() > 0) {
            retval = (SettledContract) criteria.list().get(0);
        }
        return retval;
    }

    @Override
    @Transactional
    public void updateAllRunningToFalse() {

        getCurrentSession().createSQLQuery(UPDATE_ALL_SETTLED_CONTRACT_RUNNING_TO_FALSE).executeUpdate();
    }

}
